package com.applink.sell_proof;

import java.util.Observable;
import java.util.Observer;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.ViewStub;
import android.view.Window;
import android.widget.TextView;

import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.api.DataResult;
import com.applink.sell_proof.api.Methods;
import com.applink.sell_proof.constant.Constant;
import com.applink.sell_proof.database.table.TipDBHelper;
import com.applink.sell_proof.dialog.WaitingDialog;
import com.applink.sell_proof.model.Tip;
import com.applink.sell_proof.widget.URLImageParser;

public class TipNotificationDialog extends FragmentActivity implements
		android.view.View.OnClickListener, Observer {

	private Tip mTip;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.tip_notify_dialog);

		// get serializable object
		mTip = (Tip) getIntent().getSerializableExtra(Constant.KEY_TIP);

		((TextView) findViewById(R.id.tip_name)).setText(mTip.getName());

		if (mTip.getFulltext() != null) {

			setMessage();

		} else {
			showProgressDialog(R.id.loading_container);

			// loading from API
			DataHelper.getInstance().addObserver(this);
			DataHelper.getInstance().getTipDetail(mTip.getId());
		}
		ViewStub stubView = (ViewStub) findViewById(R.id.tip_controller_bar);
		if (mTip.isSave()) {
			stubView.setLayoutResource(R.layout.close_layout);
			stubView.inflate();
		} else {
			stubView.setLayoutResource(R.layout.tip_notification_controller_bar);
			stubView.inflate();
			findViewById(R.id.btn_save).setOnClickListener(this);
		}
		findViewById(R.id.btn_close).setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_close:
			finish();
			break;
		case R.id.btn_save:
			TipDBHelper.saveTip(mTip.getId());
			DataHelper.getInstance().saveTip(mTip.getId());
			finish();
			break;
		default:
			break;
		}

	}

	@Override
	public void update(Observable observable, Object data) {
		if (data == null)
			return;
		DataResult result = (DataResult) data;

		if (result.getMethodName() == Methods.getTipDetail) {

			mTip = TipDBHelper.getTipDetail(mTip.getId());

			setMessage();

			hideProgressDialog();
		}
	}

	public void showProgressDialog(int containerId) {
		Fragment waitingDialog = new WaitingDialog(R.color.white);
		FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();
		transaction.add(containerId, waitingDialog, WaitingDialog.TAG);
		transaction.commit();
	}

	public void hideProgressDialog() {
		Fragment waitingDialog = getSupportFragmentManager().findFragmentByTag(
				WaitingDialog.TAG);
		if (waitingDialog != null)
			((DialogFragment) waitingDialog).dismiss();
	}

	private String text = "<a href=\"pdfviewer-activity://pdfvieweractivity?url=http://www.eli.sdsu.edu/courses/fall09/cs696/notes/Android13Web.pdf\">PDF doc</a>";

	private void setMessage() {
		TextView message = (TextView) findViewById(R.id.message);

		URLImageParser p = new URLImageParser(message, this);

//		if (mTip.getId() == 6) {
//			mTip.setFulltext( mTip.getFulltext() + "<p>" + text + "</p><br/>");
////			mTip.setFulltext("<br/><img src=\"http://i.i.com.com/cnwk.1d/i/tim/2013/01/29/dell-xps-13-high-res.jpg\" />"
////					+ mTip.getFulltext() + "<p>" + text + "</p><br/>");
//		}

		Spanned htmlSpan = Html.fromHtml(mTip.getFulltext(), p, null);

		message.setText(htmlSpan);
		message.setMovementMethod(LinkMovementMethod.getInstance());
	}
}
